<?php


class friend  extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
        $this->load->library('session');
        $this->load->model('connect');
        $this->load->helper("url");
        $this->load->helper("date");
        $this->load->database();
    }
    
    function request(){
          $data1 = array(
                'sender_id' => $this->session->userdata('id'),
                'receiver_id' => $_POST['id'],
                'request_type' => '1',
                
                
                'status' => 'Chưa xác nhận'
            );
            $this->db->insert('request', $data1);
            
    }
        function unfriend(){
        $a = $this->connect->isfriend($_POST['id'],$this->session->userdata('id'));
        foreach ($a as $i){
            $id = $i->relation_id;
        }
        $this->db->where('relation_id',$id);
        $this->db->delete('relationship'); 
        echo $id;
    }
    function cancelRequest(){
        $a = $this->connect->checkrequest($this->session->userdata('id'),$_POST['id']);
        foreach ($a as $i){
            $id = $i->request_id;
        }
        $this->db->where('request_id',$id);
        $this->db->delete('request');  
        echo $id;
    }
    function checkrequest(){
          $a = $this->connect->getrequest($this->session->userdata('id'));
        if (count($a) > 0) {
            echo count($a);
        } else {
            echo '';
        }
    }
    
    function  getrequest(){
         $a = $this->connect->getrequest($this->session->userdata('id'));
         foreach ($a as $i){
             $id = $i->sender_id;
             
             $b= $this->connect->getID($id);
             foreach ($b as $y){
                 $ten = $y->name;
                 $anh = $y->avarta;
             }
              echo "<li class='request-friend'>";
                echo "<div class='anhdaidien2'>";
                echo "<a href='" . base_url('getID?id=' . $id). "'>";
                echo "<img class= 'anhnho2 img-thumbnail' src='http://localhost:8080/nfa/images/avarta/" . $anh . "' >";
                echo "</a>";
                echo "</div>";
                echo "<div class ='ten2'>";
                echo "<a href='" . base_url('getID?id=' . $id). "'>";
                echo "<p style='color: #003399;'>" .$ten . "</p>";
                echo "</a>";
                echo "</div>";
                echo "<div class ='thoigian'>";
                 echo "<input type='hidden' value='".$id."' id='id-request'/>";
                echo "<div class='btn-group'>";
               
                 echo  "<button type='button' onclick='agree(".$id.");' class='btn btn-default'>Đồng ý</button>";
                 echo  "<button type='button' id='refused-requset' class='btn btn-default'>Từ chối</button>";
                    
                echo "</div>";
                echo "</div>";
                //echo "<p>".$b."</p>"; 
                echo "</liv>";
             
         }
         
    }
    function agree(){
         $data = array(
                'user_id1' => $this->session->userdata('id'),
                'user_id2' => $_POST['id'],
                'relation_type' => 'Bạn bè'
                
                
                
            );
            $this->db->insert('relationship', $data);
            
            $data2=array(
                'status' => 'Đã đồng ý'
            );
            $a =  $this->connect->checkrequest($_POST['id'],$this->session->userdata('id'));
            foreach ($a as $i){
                $b = $i->request_id;
            }
            $this->db->where('request_id',$b);
            $this->db->update('request', $data2);
            
            $now = date("Y-m-d H:i:s");
             $data3 = array(
            'id_send' => $this->session->userdata('id'),
            'id_receive' => $_POST['id'],
            'type_noti' => 3,
            'date' => $now,
            'status' => 'Chưa xem',
            'notice_id' => $b
        );
        $this->db->insert('notification', $data3);
            
            
    }
            function getbutton(){
                $a = $this->connect->isfriend($_POST['id'],$this->session->userdata('id'));
                $b =  $this->connect->requestSended($this->session->userdata('id'),$_POST['id']);
                 $c =  $this->connect->requestSended($_POST['id'],$this->session->userdata('id'));
               
                
                if($a){
                        
                           echo  "<button type='button' class='btn btn-info dropdown-toggle' data-toggle='dropdown'><span class='glyphicon glyphicon-ok'></span> Bạn bè  <span class='caret'></span></button>";

                           echo    "<ul class='dropdown-menu' role='menu'>";
                           echo     "<li><a  onclick='unfriend();' >Hủy bạn bè</a></li>";
                                   

                            echo   "</ul>";
                            echo    "<button type='button' class='btn btn-warning'><span class='glyphicon glyphicon-star'></span> Yêu thích</button>";                   
                          
                }else{
                    if($b){
                        
                           echo  "<button type='button'  class='btn btn-info dropdown-toggle' data-toggle='dropdown'> Yêu cầu đã gửi  <span class='caret'></span></button>";

                           echo    "<ul class='dropdown-menu' role='menu'>";
                           echo     "<li><a  onclick='cancelRequest();' >Hủy yêu cầu</a></li>";
                                   

                            echo   "</ul>";
                            echo    "<button type='button' class='btn btn-warning'><span class='glyphicon glyphicon-star'></span> Yêu thích</button>";
                    } elseif ($c) {
                            echo  "<button type='button' class='btn btn-info' onclick='agree(".$_POST['id'].");'  ><span class='glyphicon glyphicon-plus'></span> Đồng ý kết bạn </button>";
                            echo    "<button type='button' class='btn btn-warning'><span class='glyphicon glyphicon-star'></span> Yêu thích</button>";
                    
                }
                    else {
                        
                           echo  "<button type='button' class='btn btn-info' onclick='addfriend();'  ><span class='glyphicon glyphicon-plus'></span> Kết bạn </button>";
                            echo    "<button type='button' class='btn btn-warning'><span class='glyphicon glyphicon-star'></span> Yêu thích</button>";
                    }
                    
                }
                
                
    }
    

}

?>
